﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SharpObjects.SearchEngine.Common.Search
{
    public class TermVectorItemComparer : IComparer<ITermVectorItem>
    {
        private bool isScoreNormalized;

        public TermVectorItemComparer(bool isScoreNormalized = false)
        {
            this.isScoreNormalized = isScoreNormalized;
        }
        
        #region IComparer<ITermVectorItem> Members

        // // assume normalized values in x.Score and y.Score: range 0 to 1
        public int Compare(ITermVectorItem x, ITermVectorItem y)
        {
            if (x != null && y != null)
            {
                int diff;

                if(isScoreNormalized)
                {
                    diff = (int)(y.Score * int.MaxValue - x.Score * int.MaxValue);
                }
                else
                {
                    diff = (int)(y.Score - x.Score);
    
                }

                return diff;
            }
            else if (x == null && y == null)
            {
                return 0;
            }
            else
            {
                return int.MaxValue;
            }
        }

        #endregion
    }
}
